package org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.af;

import java.util.ArrayList;
import java.util.List;
import org.opentcs.kcvehicle.communication.kc.common.byteutils.ByteUtils;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2025/1/20 14:55
 */
public class QueryRobotStatusRsp {
    private byte[] src;
    public byte abnormal_size;
    public byte action_size;
    //预留2个字节
    public byte[] remain;
    public LocationStatusInfo locationStatusInfo;
    public RunningStatusInfo runningStatusInfo;
    public TaskStatusInfo taskStatusInfo;
    public BatteryStatusInfo batteryStatusInfo;
    public List<AbnormalEventStatusInfo> abnormalEventStatusInfoList;
    public List<ActionInfo> actionInfoList;

  public QueryRobotStatusRsp(byte[] src) {
    if (src == null) {
      throw new RuntimeException("method_QueryRobotStatusRsp_params_src_is_NUll");
    }
    this.src = src;
    this.abnormal_size = src[0];
    this.action_size = src[1];
    this.remain = ByteUtils.copyBytes(src, 2, 2); //index 2
    this.locationStatusInfo = new LocationStatusInfo(ByteUtils.copyBytes(src, 4, 32));
    this.runningStatusInfo = new RunningStatusInfo(ByteUtils.copyBytes(src, 36, 20));

        Integer pointSize = ByteUtils.toInt(src[64]);
        Integer edgeSize = ByteUtils.toInt(src[65]);
        Integer taskByteSize = 12+8*(pointSize+edgeSize);
        this.taskStatusInfo = new TaskStatusInfo(ByteUtils.copyBytes(src,56,taskByteSize));

        this.batteryStatusInfo = new BatteryStatusInfo(ByteUtils.copyBytesContainStart(src,56+taskByteSize,20));


        if(this.abnormal_size>0){
            this.abnormalEventStatusInfoList = new ArrayList<>();
            for(int i=0;i<this.abnormal_size;i++){
                int index = 56+taskByteSize+20+12*i;
              byte[] bytes = ByteUtils.copyBytesContainStart(src, index, 12);
              this.abnormalEventStatusInfoList.add(new AbnormalEventStatusInfo(bytes));
            }
        }
        if(this.action_size>0){
            this.actionInfoList = new ArrayList<>();
            for(int i=0;i<this.action_size;i++){
                this.actionInfoList.add(new ActionInfo(ByteUtils.copyBytesContainStart(src,56+taskByteSize+20+12*this.abnormal_size+12*i,12)));
            }
        }
    }
}
